



var _workdayStart = (new Date()).setHours(0,0,0,0);
var _workdayEnd = (new Date()).setHours(24,0,0,0);

var _bSupressEndMidnightWarning = false;

var _oldNonAllDayStartTime = null;
var _oldNonAllDayEndTime  = null;

var _bSuppressEvents = false;
var _millisecondsPerInterval = 1800000;
var _minutesPerInterval = 30;
var _minutesPerDay = 1440;
var _millisecondsPerMinute = 60000;
var _millisecondsPerDay = 86400000;
function suppressEvents(suppress)
{
_bSuppressEvents = suppress;
}
function GetScheduledEndValue()
{
if ((!_bSupressEndMidnightWarning) || EndCrossesMidnight())
{
return crmForm.all["scheduledend"].DataValue;
}
else
{
var oEndTime = crmForm.all["scheduledend"].DataValue;
if ((oEndTime.getHours() == 0) && (oEndTime.getMinutes() == 0))
{

oEndTime = incrementDayAcrossTimeZones(oEndTime);
}
return oEndTime;
}
}
function EndCrossesMidnight()
{
var oEndTime = crmForm.all["scheduledend"].DataValue;
var oStartTime = crmForm.all["scheduledstart"].DataValue;
if (oEndTime.getDate() > oStartTime.getDate())
{
return true;
}
return false;
}




function initDates()
{

if (crmForm.all["scheduledstart"].DataValue == null || crmForm.all["scheduledend"].DataValue == null)
{

durationChanged();
}
else if (crmForm.scheduleddurationminutes.DataValue == null)
{

endTimeChanged();
crmForm.scheduleddurationminutes.ResetToDefault();
}

crmForm.all["scheduledstart"].ResetDefault();
crmForm.all["scheduledend"].ResetDefault();
if (crmForm.isalldayevent.checked)
{
crmForm.all["scheduledstart"].ShowTime = crmForm.all["scheduledend"].ShowTime = false;
_oldNonAllDayStartTime = crmForm.all["scheduledstart"].DataValue;
_oldNonAllDayEndTime = GetScheduledEndValue();
}


attachTimeControlEvents();
}

function attachTimeControlEvents()
{
crmForm.all["scheduledend"].attachEvent("onchange", endTimeChanged);
crmForm.all["scheduledstart"].attachEvent("onchange", startTimeChanged);
crmForm.scheduleddurationminutes.attachEvent("onchange", durationChanged);
crmForm.isalldayevent.attachEvent("onclick", setAllDay);
crmForm.isalldayevent.attachEvent("onkeypress", setAllDay);
}


function setAllDay()
{


suppressEvents(true);
var oStartTime;
var oEndTime;
var numberOfDays;
try
{
if (crmForm.isalldayevent.checked)
{


crmForm.all["scheduledstart"].ShowTime = crmForm.all["scheduledend"].ShowTime = false;
crmForm.all["scheduledend"].AllDayDisplay = true;

_oldNonAllDayStartTime = crmForm.all["scheduledstart"].DataValue;
_oldNonAllDayEndTime   = GetScheduledEndValue();

if (_oldNonAllDayStartTime == null && _oldNonAllDayEndTime == null)
{

_oldNonAllDayStartTime = LocalDateTimeNow();
var minutesInDuration = crmForm.scheduleddurationminutes.DataValue;
_oldNonAllDayEndTime = new Date(_oldNonAllDayStartTime.valueOf() + crmForm.scheduleddurationminutes.DataValue * _millisecondsPerMinute);
}

if (_oldNonAllDayStartTime == null)
{
var minutesInDuration = crmForm.scheduleddurationminutes.DataValue;
_oldNonAllDayStartTime = new Date(_oldNonAllDayEndTime.valueOf() - crmForm.scheduleddurationminutes.DataValue * _millisecondsPerMinute);
}
if (_oldNonAllDayEndTime == null)
{
var minutesInDuration = crmForm.scheduleddurationminutes.DataValue;
_oldNonAllDayEndTime = new Date(_oldNonAllDayStartTime.valueOf() + crmForm.scheduleddurationminutes.DataValue * _millisecondsPerMinute);
}

var startTime = new Date(_oldNonAllDayStartTime.valueOf());
startTime.setHours(0, 0, 0, 0);
var endTime = new Date(_oldNonAllDayEndTime.valueOf());
if (endTime.getHours() != 0 || endTime.getMinutes() != 0 || endTime.valueOf() == startTime.valueOf())
{
endTime = incrementDayAcrossTimeZones(endTime);
}
crmForm.all["scheduledstart"].DataValue = startTime;
crmForm.all["scheduledend"].DataValue = endTime;
}
else
{
crmForm.all["scheduledstart"].ShowTime = crmForm.all["scheduledend"].ShowTime  = true;
if (crmForm.all["scheduledend"].AllDayDisplay)
{
crmForm.all["scheduledend"].AllDayDisplay = false;


oStartTime = crmForm.all["scheduledstart"].DataValue;
oStartTime.setHours(
_oldNonAllDayStartTime.getHours(),
_oldNonAllDayStartTime.getMinutes(),
0,
0);
crmForm.all["scheduledstart"].DataValue = oStartTime;

oEndTime = crmForm.all["scheduledend"].DataValue;
oEndTime = decrementDayAcrossTimeZones(oEndTime);
oEndTime.setHours(
_oldNonAllDayEndTime.getHours(),
_oldNonAllDayEndTime.getMinutes(),
0,
0);
crmForm.all["scheduledend"].DataValue = oEndTime;
}


if (oEndTime < oStartTime)
{
crmForm.all["scheduledend"].DataValue = new Date(crmForm.all["scheduledstart"].DataValue.valueOf() + crmForm.scheduleddurationminutes.DataValue * 60000);
}
}

setDuration(crmForm.scheduleddurationminutes , crmForm.isalldayevent.checked);
}
catch (e)
{
}
finally
{
suppressEvents(false);
}
}







function setDuration(oDuration, bIsAllDay)
{
var timeStart = crmForm.all["scheduledstart"].DataValue;
var timeEnd   = GetScheduledEndValue();
if (bIsAllDay)
{


var minutesPerWorkday;
if (_workdayStart < _workdayEnd)
{
minutesPerWorkday = (_workdayEnd - _workdayStart) / 60000;
}
else
{



var dayStart = new Date(_workdayStart.getYear(), _workdayStart.getMonth(), _workdayStart.getDate(), 0, 0, 0, 0);
var dayEnd   = new Date(dayStart);
dayEnd.setDate(dayEnd.getDate() + 1);
minutesPerWorkday = (((_workdayEnd - dayStart) + (dayEnd - _workdayStart)) / 60 / 1000);
}
if (IsNull(timeEnd) || IsNull(timeStart))
{
oDuration.DataValue = 0;
}
else
{

oDuration.DataValue = ((timeEnd - timeStart) / _millisecondsPerDay) * minutesPerWorkday - (timeEnd.getTimezoneOffset() - timeStart.getTimezoneOffset());
}
}
else
{
if (IsNull(timeEnd) || IsNull(timeStart))
{
oDuration.DataValue = 0;
}
else
{
oDuration.DataValue = ((((timeEnd - timeStart) / 60) / 1000)) - (timeEnd.getTimezoneOffset() - timeStart.getTimezoneOffset());
}
}
}
function startTimeChanged()
{

if (crmForm.all["scheduledstart"].Disabled || crmForm.all["scheduledend"].Disabled || crmForm.scheduleddurationminutes.Disabled)
{
return;
}
if (_bSuppressEvents) return;

if (crmForm.all["scheduledstart"].DataValue == null || crmForm.all["scheduledend"].DataValue == null)
{
return;
}



suppressEvents(true);
try
{



crmForm.all["scheduledend"].DataValue = endDateAcrossTimeZones();
setDuration(crmForm.scheduleddurationminutes, crmForm.isalldayevent.checked);
}
catch (e)
{
}
finally
{
suppressEvents(false);
}
}
function endTimeChanged()
{

if (crmForm.all["scheduledstart"].Disabled || crmForm.all["scheduledend"].Disabled || crmForm.scheduleddurationminutes.Disabled)
{
return;
}
if (_bSuppressEvents) return;

if (crmForm.all["scheduledstart"].DataValue == null || crmForm.all["scheduledend"].DataValue == null)
{
return;
}



suppressEvents(true);
try
{


if ((GetScheduledEndValue() < crmForm.all["scheduledstart"].DataValue) ||
(crmForm.isalldayevent.checked && (GetScheduledEndValue().toString() == crmForm.all["scheduledstart"].DataValue.toString())))
{
alert(LOCID_SPECIFY_END_TIME);

crmForm.all["scheduledend"].DataValue = new Date(crmForm.all["scheduledstart"].DataValue.valueOf() + crmForm.scheduleddurationminutes.DataValue * 60000);
}
else
{

setDuration(crmForm.scheduleddurationminutes, crmForm.isalldayevent.checked);
}
}
catch (e)
{
}
finally
{

suppressEvents(false);
}
}
function durationChanged()
{

if (crmForm.all["scheduledstart"].Disabled || crmForm.all["scheduledend"].Disabled || crmForm.scheduleddurationminutes.Disabled)
{
return;
}
if (_bSuppressEvents) return;


suppressEvents(true);
try
{

if (crmForm.all["scheduledstart"].DataValue == null)
{

if (crmForm.all["scheduledend"].DataValue == null)
{

setStartEndToNow();
}
else
{
var minutesInDuration = crmForm.scheduleddurationminutes.DataValue;
crmForm.all["scheduledstart"].DataValue = new Date(GetScheduledEndValue().valueOf() - crmForm.scheduleddurationminutes.DataValue * _millisecondsPerMinute);
}
}
var minutesInDuration = crmForm.scheduleddurationminutes.DataValue;
if ((minutesInDuration < _minutesPerDay) && (crmForm.isalldayevent.checked))
{

crmForm.isalldayevent.checked = false;
_oldNonAllDayStartTime = new Date(crmForm.all["scheduledstart"].DataValue.valueOf());
_oldNonAllDayEndTime = new Date(crmForm.all["scheduledstart"].DataValue.valueOf() + crmForm.scheduleddurationminutes.DataValue * _millisecondsPerMinute);
setAllDay();

suppressEvents(true);
}

crmForm.all["scheduledend"].DataValue =  endDateAcrossTimeZones();
}
catch (e)
{
}
finally
{
suppressEvents(false);
}
}
function endDateAcrossTimeZones()
{
var oStartDate = new Date(crmForm.all["scheduledstart"].DataValue.valueOf());
var oEndDate = new Date(crmForm.all["scheduledstart"].DataValue.valueOf() + crmForm.scheduleddurationminutes.DataValue * _millisecondsPerMinute);
var daysInDuration = Math.round(crmForm.scheduleddurationminutes.DataValue/_minutesPerDay);

if (crmForm.isalldayevent.checked)
{

if (dayDiff(oStartDate, oEndDate) < daysInDuration)
{
oEndDate = incrementDayAcrossTimeZones(oEndDate);
}
var timeDiff = oEndDate.valueOf() - oStartDate.valueOf();
if (timeDiff < _millisecondsPerDay)
{
oEndDate = new Date(oEndDate.valueOf() + timeDiff);
}
}
return oEndDate;
}
function setStartEndToNow()
{
var now = (LocalDateTimeNow()).valueOf();
var startTime = now + (_millisecondsPerInterval - (now % _millisecondsPerInterval));
crmForm.all["scheduledstart"].DataValue = new Date(startTime);
crmForm.all["scheduledend"].DataValue = new Date(crmForm.all["scheduledstart"].DataValue.valueOf() + crmForm.scheduleddurationminutes.DataValue * 60000);
}
